package ru.spbu.math.android.cbrf.dataprovider.randomprovider;

import java.util.ArrayList;
import java.util.List;

import ru.spbu.math.android.cbrf.model.Category;
import ru.spbu.math.android.cbrf.model.RateType;
import ru.spbu.math.android.cbrf.model.StockItem;

/**
 * Class for keeping data about all available RateTypes for RandomProvider
 * 
 */

public class RateTypeStore {

	private static RateTypeStore instance;

	private static final String DEMO = "Demo";

	private static final String RND = "RND";
	private static final String RE = "RE";
	private static final String UE = "UE";

	private ArrayList<RateType> rateTypeList;

	private RateTypeStore() {
		rateTypeList = new ArrayList<RateType>();

		RateType rateType = new RateType(new StockItem(RND, "Random", 1, ""),
				new StockItem("RUB", "Russian Ruble", 1, ""), "1");
		rateTypeList.add(rateType);
		rateType = new RateType(new StockItem(RE, "Regular Exception", 1, ""),
				new StockItem("RUB", "Russian Ruble", 1, ""), "2");
		rateTypeList.add(rateType);
		rateType = new RateType(
				new StockItem(UE, "Unregular Exception", 1, ""), new StockItem(
						"RUB", "Russian Ruble", 1, ""), "3");
		rateTypeList.add(rateType);
	}

	public static RateTypeStore Instance() {
		if (instance == null) {
			instance = new RateTypeStore();
		}
		return instance;
	}

	public List<Category> getCategories() {
		ArrayList<Category> list = new ArrayList<Category>();
		list.add(new Category(DEMO));
		return list;
	}

	public List<RateType> getRateTypeByCategory(Category category) {
		if (category.getName().equals(DEMO))
			return rateTypeList;

		return null;
	}

	public RateType findRateTypeById(String id) {
		for (RateType rateType : instance.rateTypeList) {
			if (rateType.getId().equals(id)) {
				return rateType;
			}
		}
		return null;
	}

	/**
	 * @param id
	 * @return positive if this RateType throws exception, negative if this
	 *         RateType never throws exception and 0 if this RateType sometimes
	 *         throws exception
	 */
	public int throwException(String id) {
		RateType rateType = findRateTypeById(id);
		if (rateType == null) {
			return 1;
		}
		
		if (rateType.getSource().getCode().equals(RND)) {
			return -1;
		} else if (rateType.getSource().getCode().equals(RE)) {
			return 1;
		} else {
			return 0;
		}

	}

}
